package cn.ffcs.uec.staff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.staff.dao.UecPsnjobDao;
import cn.ffcs.uec.staff.model.UecPsnjob;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecPsnjobDao")
@Transactional
public class UecPsnjobDaoImpl extends BaseDaoImpl implements UecPsnjobDao {

	@Override
	public PageInfo queryPageInfoByUecPsnjob(UecPsnjob uecPsnjob, int current, int pageSize) {
		if (null != uecPsnjob) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_PSNJOB T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			//判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecPsnjob.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}
			
			if (!StrUtil.isNullOrEmpty(uecPsnjob.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecPsnjob.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecPsnjob.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecPsnjob.getStaffId());
			}
			sb.append(" ORDER BY T.Modifiedtime DESC ");
			return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecPsnjob.class);
		}
		return null;
	}

	@Override
	public UecPsnjob queryUecPsnjob(UecPsnjob uecPsnjob) {
		if (null != uecPsnjob) {
			StringBuilder sb = new StringBuilder(
					" SELECT T.PSNJOB_ID,T.CLERKCODE   clerkcode , T.JOBGLBDEF2 jobglbdef2,T.JOBGLBDEF3 jobglbdef3, "
							+ " T.JOBGLBDEF4    jobglbdef4  ,T2.NAME      pkDept,T11.POSTNAME pkPost,"
							+ " T12.POSTSERIESNAME pkPostseries,T13.NAME pkPsncl,JA3.NAME    pkPsndoc"
							+ " FROM UEC_PSNJOB T " 
							+ " LEFT JOIN UEC_ORG_DEPT T2 ON (T.PK_DEPT = T2.PK_DEPT) "
							+ " LEFT JOIN Uec_Post T11  ON (T.PK_POST = T11.PK_POST)"
							+ " LEFT JOIN LDAPM_MPW_OM_POSTSERIES T12  ON (T.PK_POSTSERIES = T12.PK_POSTSERIES AND t.month_id = t12.month_id)"
							+ " LEFT JOIN LDAPM_MPW_BD_PSNCL T13  ON (T.PK_PSNCL = T13.PK_PSNCL AND t.month_id = t13.month_id)"
							+ " LEFT JOIN UEC_STAFF_INFO JA3 ON (T.PK_PSNDOC = JA3.PK_PSNDOC AND t.month_id = ja3.month_id)"
							+ " WHERE T.POSTSTAT='Y' ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			if (!StrUtil.isNullOrEmpty(uecPsnjob.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecPsnjob.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecPsnjob.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecPsnjob.getStaffId());
			}
			return this.jdbcFindList(sb.toString(), params, UecPsnjob.class).get(0);
		}
		return null;
	}

	@Override
	public UecPsnjob findUecPsnjob(UecPsnjob uecPsnjob) {
		if (null != uecPsnjob) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_PSNJOB T WHERE T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if (!StrUtil.isNullOrEmpty(uecPsnjob.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecPsnjob.getStaffId());
			}
			if (!StrUtil.isNullOrEmpty(uecPsnjob.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(uecPsnjob.getNmStaffType());
			}
			if (!StrUtil.isNullOrEmpty(uecPsnjob.getPkPsndoc())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecPsnjob.getPkPsndoc());
			}
			
			//为了确定唯一值
			sb.append(" AND T.ISMAINJOB = ? AND ENDFLAG=?");
			params.add("Y");
			params.add("N");
			List<UecPsnjob> uecPsnjobs = this.jdbcFindList(sb.toString(), params, UecPsnjob.class);
			if (uecPsnjobs.size() > 0) {
				return uecPsnjobs.get(0);
			} 
		}
		return null;
	}

	@Override
	public UecPsnjob findUecPsnjobInProcess(UecPsnjob uecPsnjob) {
		if (null != uecPsnjob) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_PSNJOB T WHERE 1=1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_PROCESS);

			if (!StrUtil.isNullOrEmpty(uecPsnjob.getId())) {
				sb.append(" AND T.PSNJOB_ID = ? ");
				params.add(uecPsnjob.getId());
			}

			List<UecPsnjob> uecPsnjobs = this.jdbcFindList(sb.toString(), params, UecPsnjob.class);

			if (uecPsnjobs.size() > 0) {
				return uecPsnjobs.get(0);
			} else {
				return null;
			}
		}

		return null;
	}

	@Override
	public UecPsnjob findUecPsnjobById(UecPsnjob uecPsnjob) {

		if (null != uecPsnjob) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_PSNJOB T WHERE 1=1 ");
			List<Object> params = new ArrayList<Object>();
			if (!StrUtil.isNullOrEmpty(uecPsnjob.getId())) {
				sb.append(" AND T.PSNJOB_ID = ? ");
				params.add(uecPsnjob.getId());
			}
			List<UecPsnjob> uecPsnjobs = this.jdbcFindList(sb.toString(), params, UecPsnjob.class);
			if (uecPsnjobs.size() > 0) {
				return uecPsnjobs.get(0);
			} else {
				return null;
			}
		}

		return null;

	}

	@Override
	public UecPsnjob findUecPsnjobOnApproval(UecPsnjob uecPsnjob) {

		if (null != uecPsnjob) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_PSNJOB T WHERE 1=1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_PROCESS);
			if (!StrUtil.isNullOrEmpty(uecPsnjob.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecPsnjob.getStaffId());
			}
			List<UecPsnjob> uecPsnjobs = this.jdbcFindList(sb.toString(), params, UecPsnjob.class);
			if (uecPsnjobs.size() > 0) {
				return uecPsnjobs.get(0);
			} else {
				return null;
			}
		}

		return null;

	}

	@Override
	public UecPsnjob findNewUecPsnjob(UecPsnjob uecPsnjob) {
		if (null != uecPsnjob) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_PSNJOB T "
					+ " WHERE T.STATUS_CD = ? AND T.POSTSTAT = 'Y' AND  T.ISMAINJOB = 'Y'  AND T.ENDFLAG = 'N'");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if (!StrUtil.isNullOrEmpty(uecPsnjob.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecPsnjob.getStaffId());
			}
			if (!StrUtil.isNullOrEmpty(uecPsnjob.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(uecPsnjob.getNmStaffType());
			}
			if (!StrUtil.isNullOrEmpty(uecPsnjob.getPkPsndoc())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecPsnjob.getPkPsndoc());
			}
			List<UecPsnjob> uecPsnjobs = this.jdbcFindList(sb.toString(), params, UecPsnjob.class);
			if (uecPsnjobs.size() > 0) {
				return uecPsnjobs.get(0);
			} 
		}
		return null;
	}

}
